//
//  JSTextLabel.m
//  JigSaw
//
//  Created by Slim Wang on 15/3/19.
//  Copyright (c) 2015年 LITB. All rights reserved.
//

#import "JSTextLabel.h"
#import "UIColor+Help.h"
@interface JSTextLabel ()

@property(nonatomic, strong)CAShapeLayer* dashLayer;

@end

@implementation JSTextLabel

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.numberOfLines = 0;
        self.userInteractionEnabled = YES;
        [self addDashBorder];
        [self.dashLayer addAnimation:[self opacityForever_Animation:0.5f repeatCount:3] forKey:nil];
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    if ([self.delegate respondsToSelector:@selector(textLabelTouchBegan:)]) {
        [self.delegate textLabelTouchBegan:self];
    }
}


- (void)addDashBorder
{
    if (!self.dashLayer) {
        self.dashLayer = [CAShapeLayer layer];
        CGMutablePathRef path = CGPathCreateMutable();
        CGRect frame = self.bounds;
        CGFloat cornerRadius = 0;
        //drawing a border around a view
        CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius);
        CGPathAddLineToPoint(path, NULL, 0, cornerRadius);
        CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO);
        CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0);
        CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO);
        CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius);
        CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO);
        CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height);
        CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO);
        
        //path is set as the layer object's path
        self.dashLayer.path = path;
        CGPathRelease(path);
        self.dashLayer.backgroundColor = [[UIColor clearColor] CGColor];
        self.dashLayer.frame = frame;
        self.dashLayer.masksToBounds = NO;
        [self.dashLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
        self.dashLayer.fillColor = [[UIColor clearColor] CGColor];
        self.dashLayer.strokeColor = [[UIColor colorWithHex:0xffe049] CGColor];
        self.dashLayer.lineWidth = 0.5;
        self.dashLayer.lineDashPattern = @[@4, @4];
        self.dashLayer.lineCap = kCALineCapRound;
    }
    [self.layer addSublayer:self.dashLayer];
    self.layer.masksToBounds = NO;
}

- (void)removeDashBorder
{
    [self.dashLayer removeFromSuperlayer];
}

- (void)showBorder
{
    self.dashLayer.opaque = NO;
}

- (void)hideBorder
{
    self.dashLayer.opaque = YES;
}


- (CABasicAnimation*)opacityForever_Animation:(float)time repeatCount:(NSInteger)repeatCount
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat:1.0f];
    animation.autoreverses = YES ;
    animation.duration = time;
    animation.repeatCount = repeatCount;
    animation.removedOnCompletion = NO ;
    animation.fillMode = kCAFillModeForwards ;
    animation.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    return animation;
}

@end
